#pragma once
#include <WinSock2.h>
#include <string>
// #include "el_export.h"

namespace el
{
	typedef int FD;

	class SocketAddrIn
	{
	public:
		SocketAddrIn();
		SocketAddrIn(const sockaddr_in &addr);
		SocketAddrIn(unsigned long ip, unsigned short port);
		SocketAddrIn(std::string ip, unsigned short port);
		SocketAddrIn(unsigned short port);
		~SocketAddrIn();

		std::string getAddrString() const;
		const sockaddr_in& sockaddr()const ;
		sockaddr_in& sockaddr();
		int lenght()const;
		int& lenght();

		unsigned short getPort();
		std::string getIP();

		bool sameAddr(SocketAddrIn& addr);

	private:
		sockaddr_in addr_;
		int length;
	};

	class Socket
	{
	public:
		static FD getTcpSocket();
		static FD getUdpSocket();
		static int getSocketError(FD fd);

		explicit Socket(FD fd);
		~Socket();

		FD fd();
		int bind(const SocketAddrIn& addr);
		int listen(int backlog = 5);
		int connect(const SocketAddrIn& addr);
		FD accept(SocketAddrIn& addr);

		int reuserAddr(bool b);
		int nonBlockSocket(bool b);
		int tcpNonDelay(bool b);
		int tcpKeepAlive(bool b);
		int tcpNodelay(bool b);
		int shutdownRead();
		int shutdownWrite();
		int getSocketError();
		int closeFD();
		bool isSelfConnect();

	private:
		FD fd_;
	};

}


